using System;

namespace Clock
{
    class Program
    {
        class Clock
        {

            DateTime alarmTime = DateTime.Now;

            public delegate void AlarmHandler(object sender, DateTime args);
            public delegate void TickHanlder(object sender, DateTime args);

            public event AlarmHandler OnAlarm;
            public event TickHanlder OnTick;

            public Clock()
            {
                OnAlarm += Alarm;
                OnTick += Tick;
            }

            public void Alarm(object sender, DateTime time)
            {
                Console.WriteLine("您的定时" + time + "到了.");
            }

            public void Tick(object sender, DateTime time)
            {
                Console.WriteLine("现在是" + time);
            }

            public void Start()
            {
                while (true)
                {
                    DateTime now = DateTime.Now;
                    OnTick(this, now);
                    if (now.ToString() == alarmTime.ToString())//tick事件始终发生
                    {
                        OnAlarm(this, alarmTime);
                    }
                    System.Threading.Thread.Sleep(1000);//时钟休眠1000ns，即每秒重新获取事件
                }
            }

            public void SetAlarmTime(DateTime atime)
            {
                Console.WriteLine(atime);//定时
                alarmTime = atime;
            }
        }
        static void Main(string[] args)
        {
            Clock clock = new Clock();
            DateTime atime = new DateTime();
            atime = DateTime.Now.AddSeconds(10);//定时，在时间基础上+10秒
            clock.SetAlarmTime(atime);
            clock.Start();
        }
    }
}